package com.controller.zk;

import com.bean.zk.Emp;
import com.bean.zk.VerificationCode;
import com.dao.zk.EmpDAO;
import com.dao.zk.HousehodeDAO;
import com.service.zk.EmpService;
import com.util.zk.AliCloudeSendSms;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Controller
public class ForgotController {

private String phonenum;

@Resource
private HousehodeDAO househodeDAO;
@Resource
private EmpService empService;

    @RequestMapping("/page/sendmsg")
    @ResponseBody
    public VerificationCode sendMessage(String phonenum) throws ExecutionException, InterruptedException {
        this.phonenum = phonenum;
        String msg = Math.round((Math.random() + 1) * 1000) + "";
        boolean result =  AliCloudeSendSms.sendMsg(phonenum,msg);
        System.out.println(result);
        VerificationCode vc = new VerificationCode();
        vc.setCode(msg);
        vc.setState(result);
        return  vc;
    }


    @RequestMapping("/page/changepswd")
    @ResponseBody
    public int change(String pswd)  {
        //在用户表查询
        int effeect = 0;
        Map<String, Object> hs = househodeDAO.selectByPhone(this.phonenum);
        Emp emp = new Emp();
        emp.setPhone(this.phonenum);
        emp.setPass(pswd);
        Map<String, Object> empMap = empService.selectByPhone(emp);
        if (hs != null ) {
            return  househodeDAO.updatePswd(pswd,this.phonenum);
        }else if (empMap != null){
            return empService.updatePswd(emp);
        }
        //更新密码
        return effeect;
    }

//    验证码验证成功后保存到seesions中
    @RequestMapping("/page/savecode-session")
    @ResponseBody
    public void savecode(String code, HttpServletRequest request)  {
        request.getSession().setAttribute("code", code);
    }


}
